//
//  PSDeckTests.m
//  PrefCore
//
//  Created by Sergey on 1/15/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "PSDeck.h"

@interface PSDeckTests : XCTestCase

-(void)checkDeckUniqueCards;
-(void)checkDeckRandomization;

@end

@implementation PSDeckTests

-(void)testDeckInstance {
    XCTAssertNotNil([PSDeck instance]);
}

-(void)testUniqueCards {
    [self checkDeckUniqueCards];
}

-(void)testShuffle {
    PSDeck *deck = [PSDeck instance];
    for (int i = 0 ; i != 100 ; i++) {
        [deck shuffle];
        [self checkDeckUniqueCards];
        [self checkDeckRandomization];
    }
}

-(void)checkDeckUniqueCards {
    NSMutableSet *set = [NSMutableSet set];
    PSDeck *deck = [PSDeck instance];
    for (int i = 0 ; i != PS_DECK_SIZE ; i++) {
        [set addObject:[deck cardAtIndex:i]];
    }
    XCTAssertEqual(set.count, (NSUInteger)32);
}

-(void)checkDeckRandomization {
    PSDeck *deck = [PSDeck instance];
    int inversionCount = 0;
    for (int i = 0 ; i != PS_DECK_SIZE ; i++) {
        PSCard *left = [deck cardAtIndex:i];
        for (int j = i+1 ; j != PS_DECK_SIZE ; j++) {
            PSCard *right = [deck cardAtIndex:j];
            if (left.compressed > right.compressed) {
                inversionCount++;
            }
        }
    }
    // This test may occasionally trip, but the probability of it is very low.
    // Rerunning the test should fix this problem.
    XCTAssert(inversionCount > 20, @"Unusually low inversion count detected.");
}

@end
